package xyz.yuanjilu.utils;

import java.util.Map.Entry;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 线程工具类
 * @author kkomge@qq.com
 */
public class ThreadUtils {
	
	 static Logger logger =LoggerFactory.getLogger(ThreadUtils.class);
	 /**
	  * 在日志中输出当前JVM的线程情况
	  */
	 public static void showJVMTreadsTrace(){
		 Set<Entry<Thread, StackTraceElement[]>> stackTraceSet = Thread.getAllStackTraces().entrySet();
		 
		 for (Entry<Thread, StackTraceElement[]> stackTrace : stackTraceSet) {
			 Thread thread = (Thread) stackTrace.getKey();  
	            StackTraceElement[] stack = (StackTraceElement[]) stackTrace.getValue();  
	            if(thread.equals(Thread.currentThread())){  
	                continue;  
	            }  
	            logger.info("线程：" + thread.getClass()+" name:"+thread.getName());  
	            if (logger.isDebugEnabled()) {
	            	for (StackTraceElement element : stack){  
		            	logger.info("" + element);  
		            }  
				}
		}
	 }
}
